﻿using Logger;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Core;

namespace XiaoweiUWP.ViewModels
{
    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected CoreDispatcher dispatcher { get; set; } = null;

        protected event Action DispatcherChanged;

        public void SetDispatcher(CoreDispatcher dispatcher)
        {
            this.dispatcher = dispatcher;
            if (dispatcher == null)
            {
                PropertyChanged = null;
            }
            DispatcherChanged?.Invoke();
        }
        protected void UpdateValue(string name)
        {
            if (dispatcher == null)
            {
                return;
            }
            _ = dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                () =>
                {
                    Debug.WriteLine($"{this.GetType()} Dispatcher：UpdateValue {name}");
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
                    Debug.WriteLine($"{this.GetType()} Dispatcher end：UpdateValue {name}");
                }
                );
        }

        public abstract void Dispose();
    }
}
